package com.sys.server.modules.controller;

import com.sys.server.common.annotation.Authentication;
import com.sys.server.common.annotation.Log;
import com.sys.server.common.constants.enums.ResultSetEnum;
import com.sys.server.common.model.R;
import com.sys.server.modules.controller.dto.UserDto;
import com.sys.server.modules.entity.UserEntity;
import com.sys.server.modules.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Authentication
@RestController
@Api(tags = "用户管理模块")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation("用户管理模块--分页获取用户列表")
    @RequestMapping(value = "/getUserList", method = RequestMethod.GET)
    public R getUserList(@ApiParam(value = "当前页") @RequestParam Long page,
                         @ApiParam(value = "每页记录条数") @RequestParam Long limit) {
        return userService.getUserList(page, limit);
    }

    @ApiOperation("用户管理模块--获取用户信息")
    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    public R getUserInfo(@ApiParam(value = "用户token") @RequestParam String token) {
        UserEntity userEntity = userService.getUserInfo(token);
        Map<String, Object> map = new HashMap<>();
        map.put("uid", userEntity.getUid());
        map.put("name", userEntity.getName());
        String[] role = new String[1];
        role[0] = userEntity.getRole();
        map.put("role", role);
        map.put("department", userEntity.getDepartment());
        map.put("contact", userEntity.getContact());
        return R.ok(ResultSetEnum.SUCCESS, map);
    }

    @ApiOperation("用户管理模块--添加单个用户")
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @Log(module = "用户管理", action = "添加单个用户", type = "新增")
    public R addUser(@ApiParam(value = "单个用户信息") @RequestBody UserDto userDto) {
        return userService.addUser(userDto);
    }

    @ApiOperation(value = "用户管理模块--返回指定角色的用户")
    @RequestMapping(value = "/getUserListByRole", method = RequestMethod.GET)
    public R getUserListByRole(@ApiParam(value = "用户角色") @RequestParam String role,
                               @ApiParam(value = "当前页") @RequestParam Long page,
                               @ApiParam(value = "记录数") @RequestParam Long limit) {
        return userService.getUserListByRole(role, page, limit);
    }

    @ApiOperation(value = "用户管理模块--更新用户信息")
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    @Log(module = "用户管理", action = "更新用户信息", type = "修改")
    public R updateUserInfo(@ApiParam(value = "用户信息") @RequestBody UserDto userDto) {
        return userService.updateUserInfo(userDto);
    }

    @ApiOperation(value = "用户管理模块--重置用户密码")
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    @Log(module = "用户管理", action = "重置密码", type = "修改")
    public R resetPassword(@ApiParam(value = "用户信息") @RequestBody UserDto userDto) {
        return userService.resetPassword(userDto);
    }

    @ApiOperation(value = "用户管理模块--批量导入用户")
    @PostMapping(value = "/importUsers")
    @Log(module = "用户管理", action = "批量导入用户", type = "导入")
    public R importUsers(MultipartFile file) throws IOException {
        return userService.importUsers(file);
    }

}
